package com.heima.admin.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveServer;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.dtos.SensitiveDto;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdSensitiveServerImpl extends ServiceImpl<AdSensitiveMapper,AdSensitive> implements AdSensitiveServer {


    @Override
    public ResponseResult list(SensitiveDto sensitiveDto){
        if (ObjectUtil.isEmpty(sensitiveDto)){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        Page page = new Page<>(sensitiveDto.getPage(), sensitiveDto.getSize());
        LambdaQueryWrapper<AdSensitive> lambdaQuery = Wrappers.lambdaQuery();
        if (StrUtil.isNotEmpty(sensitiveDto.getSensitives())){
            lambdaQuery.like(AdSensitive::getSensitives,sensitiveDto.getSensitives());
        }
       IPage iPage = page(page,lambdaQuery);

        ResponseResult result = new PageResponseResult(sensitiveDto.getPage(), sensitiveDto.getSize(), (int)iPage.getTotal());

        result.setData(iPage.getRecords());

        return result;

    }


    @Override
    public ResponseResult insert(AdSensitive adSensitive){
        if (ObjectUtil.isEmpty(adSensitive)){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        adSensitive.setCreatedTime(new Date());
        boolean sav = save(adSensitive);
        if (sav){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }

        return ResponseResult.errorResult(AppHttpCodeEnum.ERROR);
    }


    @Override
    public ResponseResult update(AdSensitive adSensitive){
        if (ObjectUtil.isEmpty(adSensitive) || adSensitive.getId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        boolean update = updateById(adSensitive);

        if (update){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.ERROR);
    }


    @Override
    public ResponseResult deleteById(Integer id){
        if (id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        AdSensitive byId = getById(id);
        if (byId == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        boolean removeById = removeById(id);
        if (removeById){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.ERROR);
    }
}
